In a running process, a program usually calls some resources such as memory, a file handle, a semaphore, a message capsule, and a database connection pool, and after running of the program ends, the called resources need to be released in time. A resource leak refers to that due to a problem such as release omission or invalid memory access, a program cannot release resources that are no longer used, and consequently the resources cannot be reused. As resources are constantly leaked, there are fewer available resources in a system, and as a result, system performance degrades, and finally, the system even cannot provide a service or process a service because the resources are insufficient.
Typically, during memory allocation, a quantity of times memory is allocated and a size of allocated memory are recorded in a log file, during memory release, a quantity of times memory is released and a size of released memory are recorded in a log file, and a quantity of times memory is not released and a size of unreleased memory are determined according to the quantity of times memory is allocated, the size of allocated memory, the quantity of times memory is released, and the size of released memory that are recorded. In this way, a memory leak is detected. All memory allocation and memory release need to be tracked and recorded, which causes high detection overheads and low efficiency. However, these methods can only be used at a test stage or a debug stage of a program, and memory leak detection cannot be performed in an online environment in which the program actually runs, a memory leak that is relatively difficult to find and that occurs when the program actually runs cannot be detected, and an undetected memory leak causes damage to a service or even interrupts the service.